﻿





using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework;
using GlobalGameClasses;


namespace InterfaceComponents
{
    /// <summary>
    /// The textbox enables the user to enter text.
    /// </summary>
    public class TextBox : InterfaceComponent, MenuComponent
    {
	    public string input
	    {
		    get;
		    private set;
	    }

        private Texture2D line;

	    private SpriteFont spriteFont;

        private TimeSpan backspaceTime;

	    public event GlobalGameClasses.Eventhandler.ConfirmLogin confirmInput;

	    private int maxLength
	    {
		    get;
		    set;
	    }

	    public TextBox(int maxLength, Vector2 position, SpriteBatch spriteBatch, SpriteFont spriteFont)
	    {
            this.maxLength = maxLength;
            this.position = position;
            this.spriteBatch = spriteBatch;
            this.spriteFont = spriteFont;
            //create white texture to display the textbox if the input is empty
            line = new Texture2D(spriteBatch.GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
            line.SetData(new[] { Color.White });
            backspaceTime = new TimeSpan(0);
            input = "";
	    }

        /// <summary>
        /// Checks which characters the user typed and throws the confirmInput event if the User pressed the Enter key.
        /// </summary>
        public void Update(KeyboardState keyboard, KeyboardState oldKeyboard, MouseState mouse, MouseState oldMouse, bool selected)
        {
            Keys[] keys = oldKeyboard.GetPressedKeys();
            string text = "";
            for (int i = 0; i < keys.Length; i++)
            {
                if (KeyPressed(keyboard, oldKeyboard, keys[i]))
                {
                    char character;
                    if (keys[i] == Keys.OemTilde)
                    {
                        character = 'Ö';
                    }
                    else if (keys[i] == Keys.OemQuotes)
                    {
                        character = 'Ä';
                    }
                    else if (keys[i] == Keys.OemSemicolon)
                    {
                        character = 'Ü';
                    }
                    else if (keys[i] == Keys.OemOpenBrackets)
                    {
                        character = 'ß';
                    }
                    else
                    {
                        character = (char)keys[i].GetHashCode();
                    }
                    if (isLegitCharacter(character) && input.Length + text.Length < maxLength)
                    {
                        text += character;
                    }
                }
            }
            if (keys.Contains(Keys.LeftShift) || keys.Contains(Keys.RightShift))
            {
                input += text;
            }
            else
            {
                input += text.ToLower();
            }
            if (keys.Contains(Keys.Back) && input.Length > 0 && GlobalGameValues.Values.gameTime.TotalGameTime.Subtract(backspaceTime).Milliseconds > 100)
            {
                backspaceTime = GlobalGameValues.Values.gameTime.TotalGameTime;
                input = input.Substring(0, input.Length - 1);
            }
            if (KeyPressed(keyboard, oldKeyboard, Keys.Enter))
            {
                confirmInput(this);
            }
        }

        /// <summary>
        /// Draws the textbox with the specified color.
        /// </summary>
        /// <param name="color">Color of the font.</param>
        public override void Draw(Color color)
        {
            spriteBatch.DrawString(spriteFont, input, position - new Vector2(2, 0), Color.Black);
            spriteBatch.DrawString(spriteFont, input, position + new Vector2(2, 0), Color.Black);
            spriteBatch.DrawString(spriteFont, input, position - new Vector2(0, 2), Color.Black);
            spriteBatch.DrawString(spriteFont, input, position + new Vector2(0, 2), Color.Black);
            spriteBatch.DrawString(spriteFont, input, position, Color.White);
            Rectangle dest = new Rectangle((int)position.X, (int)(position.Y + spriteFont.LineSpacing), (int)(spriteFont.MeasureString("Ö").X * maxLength), 1);
            spriteBatch.Draw(line, dest, color);
        }

        private bool KeyPressed(KeyboardState keyboard, KeyboardState oldKeyboard, Keys key)
        {
            return oldKeyboard.IsKeyDown(key) && keyboard.IsKeyUp(key);
        }

        private bool isLegitCharacter(char c)
        {
            return ((c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == 'Ä' || c == 'Ü' || c == 'Ö' || c == 'ß');
        }
    }
}

